package ihm;


import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.LEADING;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.concurrent.Semaphore;

import javax.swing.*;

import execution.FormatageObservation;
import execution.Observation;

/**
 * 
 * Fenetre de progression des tests 
 *
 */
public class ProgressionTests extends JFrame implements ActionListener {
	private JPanel panel1;
	private JScrollPane panel2;
	private JPanel panel3;
	private JLabel label1;
	private JLabel label2;
	private JButton affichage;
	private JButton retour;
	private JButton fermer;
	private JProgressBar progression;
	private JTextArea textArea;
	private Thread thread;
	private Resultats resultats;
	private Vector<Observation> observationVect;
	
	/**
	 * Constructeur
	 * @throws InterruptedException
	 */
	public ProgressionTests() throws InterruptedException{

		this.setTitle("Etats des tests");
		Container conteneur = this.getContentPane();
		GroupLayout layout = new GroupLayout(conteneur);
		conteneur.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);


		label1 = new JLabel("Progression des tests : ");
		progression = new JProgressBar();
		// progression.setMinimum(0);
		// progression.setMaximum(10);
		progression.setStringPainted(true);
		label2 = new JLabel("Informations sur les tests en cours : ");
		textArea = new JTextArea(30,10);
		textArea.setEditable(false);
		panel2 = new JScrollPane(textArea);
		affichage = new JButton("Affichage des resultats");
		affichage.addActionListener(this);
		affichage.setEnabled(false);
		retour = new JButton("Retour Accueil");
		retour.addActionListener(this);
		fermer = new JButton("Fermer");
		fermer.addActionListener(this);
		panel1 = new JPanel(new GridBagLayout());
		panel3 = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		//natural height, maximum width
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.ipady = 20;
		c.gridx = 0;
		c.gridy = 0;
		panel1.add(progression, c);


		c.ipady = 20;
		c.gridx = 0;
		c.gridy = 0;
		panel3.add(affichage, c);
		c.gridx = 1;
		c.insets = new Insets(0,25,0,0);
		panel3.add(retour, c);
		c.gridx = 2;
		c.insets = new Insets(0,25,0,0);
		c.anchor = GridBagConstraints.SOUTH;
		panel3.add(fermer, c);

		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(LEADING)
						.addComponent(label1)
						.addComponent(panel1)
						.addComponent(label2)
						.addComponent(panel2)
						.addComponent(panel3)
				)               
		);

		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGap(50)
				.addComponent(label1)                                   
				.addComponent(panel1)
				.addGap(30)
				.addComponent(label2)
				.addComponent(panel2)
				.addGap(30)
				.addComponent(panel3)

		);

		this.pack();
		//tuer le processus en cliquant sur la croix pour fermer la fenetre
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		
		Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		
		this.setLocation((screen.width - this.getSize().width)/2, 0);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object selected = e.getSource();
		if(selected == affichage){
			 resultats = new Resultats();
			this.setVisible(false);
			resultats.setVisible(true);
			FormatageObservation formatageobservation = 
					new FormatageObservation(observationVect, this.getResultats());
			formatageobservation.miseEnForme();
			
		}else if (selected == retour){
			this.setVisible(false);
			Accueil accueil = new Accueil();
			accueil.setVisible(true);
		}else if (selected == fermer){
			System.exit(0);

		}

	}

	/**
	 * @return
	 */
	public JProgressBar getProgression() {
		return progression;
	}

	/**
	 * @return textArea
	 */
	public JTextArea getTextArea() {
		return textArea;
	}

	/**
	 * @return resultats
	 */
	public Resultats getResultats() {
		return resultats;
	}

	public void setObservationVect(Vector<Observation> observationVect) {
		this.observationVect = observationVect;
	}

	/**
	 * @return affichage
	 */
	public JButton getAffichage() {
		return affichage;
	}

	
	

}